package cmdb_blacklist_status_v1

import (
	"context"

	cmdb_blacklist_rpc "dfx_cmdb/cmdb-blacklist-rpc/types/cmdb-blacklist-rpc"
	"dfx_cmdb/cmdb-gateway/internal/svc"
	"dfx_cmdb/cmdb-gateway/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetAllBlacklistLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetAllBlacklistLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAllBlacklistLogic {
	return &GetAllBlacklistLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetAllBlacklistLogic) GetAllBlacklist(req *types.GetAllBlacklistRequest) (resp *types.BlacklistResponse, err error) {
	// todo: add your logic here and delete this line
	grpcReq := &cmdb_blacklist_rpc.GetAllBlacklistRequest{
		Type: req.Type,
	}
	grpcResp, err := l.svcCtx.CmdbBlacklistRpc.GetAllBlacklist(l.ctx, grpcReq)
	if err != nil {
		return nil, err
	}

	httpBlackValue := make(map[string]types.StringList)
	for k, v := range grpcResp.Blacklist {
		httpBlackValue[k] = types.StringList{
			Values: v.Values,
			Port:   v.Port,
			Count:  int32(len(v.Values)),
		}
	}

	return &types.BlacklistResponse{
		Blacklist: httpBlackValue,
	}, nil
}
